package com.eryos.gwt.mailmodule.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class MailView extends Composite implements MailPresenter.View {


    /***********************  UI DEFINITION  ********************************************/
    @UiTemplate("MailView.ui.xml") 
    interface MailViewUiBinder extends UiBinder<Widget, MailView> {} 

    private static MailViewUiBinder uiBinder = GWT.create(MailViewUiBinder.class);        
    
    private static final String PUBLIC_KEY = "6Ld5OskSAAAAAAdwlooG9nm_b7tsVNH0Gr8DhR2S";

    /***********************  UI FIELDS  ************************************************/
    @UiField TextBox     nameField;
    @UiField TextBox     mailField;
    @UiField TextBox     subjectField; // @UiField listBox subjectField;
    @UiField TextArea    messageField;
    @UiField Button      validButton; 
    
    Element statusElement;

    private static final String CAPTCHA_DIV_ID = "captchaContainer";
    private static final String STATUS_DIV_ID  = "statusContainer";

    /************************************************************************************/
    /** 
     * Main constructor for View 
     */ 
    public MailView(){ 
        initWidget(uiBinder.createAndBindUi(this));
    }
    
    public void init(){
		statusElement = DOM.getElementById(STATUS_DIV_ID);
    	updateStatus("initView OK");

    	//createCaptcha(PUBLIC_KEY, CAPTCHA_DIV_ID, "white");
    	updateStatus("initCaptcha OK");
    }

    /***********************  INTERFACE METHODS  ****************************************/

    public String getNameFieldValue(){
        return nameField.getText();
    }
    public String getMailFieldValue(){
        return mailField.getText();
    }
    public String getSubjectFieldValue(){
        return subjectField.getText();
    }
    public String getMessageFieldValue(){
        return messageField.getText();
    }
    public HasClickHandlers getValidButton(){
        return validButton;
    }
    public String getCaptchaChallengeValue(){
        return getCaptchaChallenge();
    }
    public String getCaptchaResponseValue(){
        return getCaptchaResponse();
    }
    public void updateStatus(String msg){
		if ( statusElement != null ) 
			statusElement.setInnerHTML(statusElement.getInnerHTML()+"<br/>"+msg);
		else {
			System.out.println("null element");
			statusElement = DOM.getElementById(STATUS_DIV_ID);
		}
	}


    /***********************  JNSI METHODS  ****************************************/

    /**
     * Used to instantiate the captcha in the page
     */
    public static native void createCaptcha(String publicKey, String captchaDivId, String themeColor) /*-{
    		$wnd.Recaptcha.create(publicKey, captchaDivId, {
    		theme: themeColor,
    		callback: $wnd.Recaptcha.focus_response_field
    		});
    	}-*/; 

    /**
     * Retrieve the reCaptcha response value set by the user
     * @return
     */
    public static native String getCaptchaResponse() /*-{
    		return $wnd.Recaptcha.get_response();
    	}-*/;

    /**
     * Retrieve a string representation of reCaptcha Challenge
     * @return
     */
    public static native String getCaptchaChallenge() /*-{
    		return $wnd.Recaptcha.get_challenge()
    	}-*/; 
}